#include "os.h"


#define Disable_Interrupt()		asm volatile ("cli"::)
#define Enable_Interrupt()		asm volatile ("sei"::)

//#define SYSTEM_TICKS_PER_MS            8000
//#define CLOCK_PRESCALER                8

//#define CLOCK_CYCLES_PER_TICK (((unsigned int)SYSTEM_TICKS_PER_MS * TICK) / CLOCK_PRESCALER)
#define IDLE 0
#define NULL_FCFS_TASK 0xFFFF
#define FCFS                   1      
#define TIMER_FACTOR 	5
#define PROCESSOR_SPEED 8

typedef void (*voidfuncptr) (void);      /* pointer to void f(void) */
extern void CSwitch();
extern void Exit_Kernel();    /* this is the same as CSwitch() */

void initTimer(void);
void startTimer(void);
void stopTimer(void);
void initNextTask();

/* Prototype */
void Task_Terminate(void);


extern void Enter_Kernel();


/*
  * This is the set of kernel requests, i.e., a request code for each system call.
  */
typedef enum kernel_request_type
{
   NONE = 0,
   CREATE,
   NEXT,
   TERMINATE,
   TIME
} KERNEL_REQUEST_TYPE;

KERNEL_REQUEST_TYPE Kernel_Request;
/**
  * Each task is represented by a process descriptor, which contains all
  * relevant information about this task. For convenience, we also store
  * the task's stack, i.e., its workspace, in here.
  */

typedef enum task_states { 
    DEAD = 0,   
    INTERRUPTIBLE,
    WAITING,   
    READY, 
    RUNNING 
 } TASK_STATES;


typedef struct ProcessDescriptor
{
   unsigned char *sp;   /* stack pointer into the "workSpace" */
   unsigned char workSpace[WORKSPACE];
   TASK_STATES state;
   voidfuncptr  code;   /* function to be executed as a task */
   KERNEL_REQUEST_TYPE request;
   int arg;
   unsigned int level;	
  	unsigned int name;
} PROCESS_DESCRIPTOR;

 typedef struct task {   
    volatile unsigned char *sp; /* stack pointer into the "workSpace" */
    unsigned int ticks; /* clock ticks */
	unsigned int maxTicks; /*maximum number of ticks */
	unsigned int count; /* count to see how many ticks have gone by */
	int task_index;
 	
 	unsigned int level;	/* level of task, ie SYSTEM, RR or PERIODIC as defined in os.h */
  	unsigned int name;  /* name of task defined as an int so it can be recognized */
	   
   
	KERNEL_REQUEST_TYPE request;
    TASK_STATES state;
	PROCESS_DESCRIPTOR *process_desc;
   
    struct TASK_HANDLER* queueNext; /* Linked List */
	struct TASK_HANDLER* queueLast;
	struct TASK_HANDLER* owner;

 } TASK_HANDLER;
 
 volatile unsigned int Task_Count; 
 
 TASK_HANDLER* CurrentTask;
 
 volatile unsigned char *KernelSp;
 
 volatile unsigned char *CurrentSp;
 
 TASK_HANDLER Task_List[MAXPROCESS+1];
 
 typedef struct fcfsTaskList {
    unsigned int ptrIntoTaskList;
    unsigned int nextPtr;
    unsigned int prevPtr;
 } FCFS_Task;
 
 volatile unsigned int FCFS_Tail;
 
 volatile unsigned int FCFS_Head;
 
 FCFS_Task FCFS_List[MAXPROCESS+1];
 
 volatile unsigned short int FCFS_count;
 
 volatile short int periodic_task;
 
 volatile unsigned short int PERIODIC_count;
 /** index to next task to run */
volatile unsigned short int nextPPP;

 
 


